<?php
require_once dirname(__FILE__).'/include.php';
Master::loadCoreClass("AdminControl");
$Admin = new AdminControl();
$formdir = Post::getByKey("formdir");
if ($formdir =="")
	$formdir = "/";
if (!FileSystem::dirExists($formdir))
	FileSystem::makeDir($formdir);
///*******************************************************
function createHtml($id,$htmlType,$value="")
{
	$input = "<input class=\"text-input small-input\" type=\"".$htmlType."\" id=\"".$id."\" name=\"".$id."\" value=\"".$value."\" />\r\n";
	$textarea = "<textarea class=\"text-input textarea wysiwyg\" id=\"".$id."\" name=\"".$id."\" cols=\"40\" rows=\"10\">".$value."</textarea>\r\n";
	$check = "";
	if ($htmlType == "textarea")
		return $textarea;
	else
		return $input;
}
///*******************************************************
echo "正在生成表单……请稍候…… <br />";
$tableNmae = Get::getByKey("tablename");
$priKey = "";$tableHeader = "";$tableBody = "";$formBody="";$editformBody="";$priKeyForm="";
$fileditems = $Admin->listfileditems(array('tableName'=>$tableNmae));
foreach($fileditems as $k)
{
	if ($k->listshow==1)
	{
		$tableHeader = $tableHeader."<th>".$k->fieldCN."</th>\r\n";
		$tableBody = $tableBody."<td><?=\$key->".$k->fieldName."?></td>\r\n";		
		if ($k->fieldpri==1)
			$priKey = $k->fieldName;	
	}else{
		if ($k->fieldpri==1)
			$priKey = $k->fieldName;			
	}
	if ($k->fieldpri!=1)
	{
		$formBody = $formBody."<label>".$k->fieldCN."</label>\r\n";
		$formBody = $formBody.createHtml($k->fieldName,$k->htmltype);	
		$editformBody = $editformBody."<label>".$k->fieldCN."</label>\r\n";
		$editformBody = $editformBody.createHtml($k->fieldName,$k->htmltype,"<?=\$model->".$k->fieldName."?>");					
	}else{
			$priKeyForm = createHtml($k->fieldName,$k->htmltype,"<?=\$model->".$k->fieldName."?>");		
		}
}
$tableitems = $Admin->listtableitems(array('tableName'=>$tableNmae));
$tableitems  = $tableitems [0];
$readStr = FileSystem::readFile("tpl/TemplateListForm.php");
$readStr = str_replace("[TABLENAME]",$tableNmae ,$readStr);
$readStr = str_replace("[TABLENAMECN]",$tableitems->tableNameCN ,$readStr);
$readStr = str_replace("[SHOWNUMS]",count($fileditems) ,$readStr);
$readStr = str_replace("[PRIKEY]",$priKey ,$readStr);
$readStr = str_replace("<!--TABLEHEADER-->",$tableHeader ,$readStr);
$readStr = str_replace("<!--TABLEBODY-->",$tableBody ,$readStr);
$filepath = $formdir."/list".$tableNmae.".php";

if (FileSystem::fileExists($filepath))
{
	if(Utility::jsMessage("文件".$filepath."已经存在,是否覆盖？","",true))
		FileSystem::saveData($filepath,$readStr);
}else
	FileSystem::saveData($filepath,$readStr);	
echo "生成列表表单完毕……正在生成添加表单……<br />";

//******************************************************************
$readStr = FileSystem::readFile("tpl/TemplateCreatForm.php");
$readStr = str_replace("[TABLENAME]",$tableNmae ,$readStr);
$readStr = str_replace("[PRIKEY]",$priKey ,$readStr);
$readStr = str_replace("[TABLENAMECN]",$tableitems->tableNameCN ,$readStr);
$readStr = str_replace("<!--FORMBODY-->",$formBody ,$readStr);
$filepath = $formdir."/Add".$tableNmae.".php";
if (FileSystem::fileExists($filepath))
{
	if(Utility::jsMessage("文件".$filepath."已经存在：是否覆盖？","",true))
		FileSystem::saveData($filepath,$readStr);
}else{
	FileSystem::saveData($filepath,$readStr);	
}
echo "生成添加表单完毕……正在生成编辑表单……<br />";

//******************************************************************
$readStr = FileSystem::readFile("tpl/TemplateEditForm.php");
$readStr = str_replace("[TABLENAME]",$tableNmae ,$readStr);
$readStr = str_replace("[PRIKEYFOMR]",$priKeyForm,$readStr);
$readStr = str_replace("[TABLENAMECN]",$tableitems->tableNameCN ,$readStr);
$readStr = str_replace("<!--FORMBODY-->",$editformBody ,$readStr);
$filepath = $formdir."/edit".$tableNmae.".php";
if (FileSystem::fileExists($filepath))
{
	if(Utility::jsMessage("文件".$filepath."已经存在：是否覆盖？","",true))
		FileSystem::saveData($filepath,$readStr);
}else{
	FileSystem::saveData($filepath,$readStr);	
}
echo "生所有表单生成完毕。。。<br />";
Header("Location:".$formdir."/list".$tableNmae.".php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=Config::$siteName ?></title>		
<body>
</body>
